<template>
  <div class="enter-username">
    <h1>找回密码</h1>
    <p>请输入用户名</p>
    <el-form :model="form" :rules="rules" ref="ruleForm" status-icon>
      <el-form-item prop="userName">
        <el-input placeholder="用户名" style="width: 400px;" v-model.trim="form.userName"></el-input>
      </el-form-item>
      <el-form-item class="black-btn">
        <el-button @click="nextStep" style="width: 400px;margin-top: 30px;" :loading="loading">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    const validateUserName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入用户名'))
      } else {
        const params = {
          userName: value
        }
        this.$http.post(this.$service.checkUserName, params).then(res => {
          if (!res.value) {
            return callback(new Error('用户名不存在'))
          } else {
            return callback()
          }
        })
      }
    }
    return {
      form: {},
      rules: {
        userName: [{ validator: validateUserName, trigger: 'blur' }]
      },
      loading: false
    }
  },
  methods: {
    nextStep() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.loading = true
          const params = {
            username: this.form.userName
          }
          this.$http.post(this.$service.getUserInfoByUserName, params).then(res => {
            this.$emit('next-step', res.userInfoDto)
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.enter-username {
  h1 {
    font-size: 30px;
    margin: 0;
  }
  p {
    font-size: 16px;
    color: #aaa;
    margin-bottom: 40px;
  }
}
</style>
